<?php 

session_start();

if(isset($_POST['username']) && !empty($_POST['username']) && isset($_POST['password']) && !empty($_POST['password'])) {
	$plaintext1 = $_POST['username'];
	$plaintext2 = $_POST['password'];
} else {
	$_SESSION['error-message'] = 'You need to fill in the fields!';
	header('Location: login.php');
	die();
}


/******  START OF encrypting process  ******/
// Normal key
$secretKey = "c0946fdaa039df5e";
// Vector key
$ivKey = "8d9143d39a2f3121";

// Open and init the mcrypt library with the keys
$td1 = mcrypt_module_open("rijndael-128", "", "cbc", $ivKey);
mcrypt_generic_init($td1, $secretKey, $ivKey);
$td2 = mcrypt_module_open("rijndael-128", "", "cbc", $ivKey);
mcrypt_generic_init($td2, $secretKey, $ivKey);

// encrypt username and password
$crypt_text1 = mcrypt_generic($td1, $plaintext1);
$crypt_text2 = mcrypt_generic($td2, $plaintext2);

// deinit and close mcrypt module
mcrypt_generic_deinit($td1);
mcrypt_module_close($td1);
mcrypt_generic_deinit($td2);
mcrypt_module_close($td2);

$username = base64_encode($crypt_text1);
$password = base64_encode($crypt_text2);
 /******  END OF encrypting process  ******/
 
 
/******* START cURL process **********/
$handle = curl_init();

$username = urlencode($username);
$password = urlencode($password); 

curl_setopt_array(
	$handle, 
	array(
		CURLOPT_URL => 'http://davidilag.dk/2Bet/login.php',
		CURLOPT_POST => TRUE,
		CURLOPT_POSTFIELDS => 'username='.$username.'&password='.$password.'',
		CURLOPT_RETURNTRANSFER => true
	) 
);
 
$data = curl_exec($handle);
curl_close($handle);
/******* END cURL process **********/

/******** START of decrypting process  ***********/
$key = 'E9BKL1C4xaZcynCK';
$data = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($data), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
$data = json_decode($data);
/******** END of decrypting process  ***********/


// Convert obj. array to normal array in order to retreve data
$data = objectToArray($data);
function objectToArray($d) {
	if (is_object($d)) {$d = get_object_vars($d);}
	if (is_array($d)) {return array_map(__FUNCTION__, $d);}
	else {return $d;}
}

/****** For debugging purpoces ******/
$_SESSION['data'] = $data;


/********* START of making cookie to save login-token in *********/
$token = $data[0]['token'];
$tokenexpires = $data[0]['tokenexpires'];

// create the cookie 
setcookie('2Bet_token', $token, $tokenexpires);
	
// for debugging only
$_SESSION['token'] = $data[0]['token'];

/* When we want to retreve the value of the cookie and decrypt it:
$cookie_value = $_COOKIE["2Bet_token"];
$key = 'E9BKL1C4xaZcynCK';
$cookie_value = rtrim(mcrypt_decrypt(MCRYPT_RIJNDAEL_256, md5($key), base64_decode($cookie_value), MCRYPT_MODE_CBC, md5(md5($key))), "\0");
$_SESSION['cookie_value'] = $cookie_value;
*/
/********* END of making cookie to save login-token in *********/


// retreve login-status from the array that came from the login-service from login service
if($data[0]['result']) {
	include_once $_SERVER['DOCUMENT_ROOT'].'/application/library/Betting_lib.php';
	
	// set username in session att.
	$_SESSION['username'] = $data[0]['username'];
	// set userid in session att.
	$_SESSION['userid'] 		= $data[0]['userid'];
	//$local = Database::get(array('table'=>'users','fields'=>array('user_id'),'where'=>array('userid'=>$data[0]['userid']),'limit'=>1,'return_type'=>'row','log'=>true));
	// set local user_id to interact with local site
	$_SESSION['local_userid'] 	= Betting_lib::get_userid($data[0]['username'],'value');
	
	header('Location: index.php');
	exit;
	
} else {
	
	$_SESSION['username'] = NULL;
	session_unset($_SESSION['username']);
	$_SESSION['userid'] = NULL;
	session_unset($_SESSION['userid']);
	
	$_SESSION['error-message'] = 'Login failed!';
	
	header('Location: login.php');
	exit;
	
} 

?>